package com.ctshk.sapp.tour.controller;


import java.io.OutputStream;
import java.util.List;

import javax.validation.Valid;

import com.ctshk.rpc.tour.req.schedule.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.poi.utils.ExcelExportUtils;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.common.web.utils.ServletUtils;
import com.ctshk.rpc.tour.dto.schedule.ScheduleCalendarDTO;
import com.ctshk.rpc.tour.dto.schedule.SeriesAgentByBookDTO;
import com.ctshk.rpc.tour.dto.schedule.SeriesTripScheduleDTO;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleAdjustmentDTO;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleCostDTO;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleDTO;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleDetailsDTO;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleGroupDTO;
import com.ctshk.rpc.tour.dto.schedule.TripScheduleListDTO;
import com.ctshk.rpc.tour.service.ISeriesTripScheduleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 旅行团团期 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-20
 */
@RestController
@RequestMapping("/series/trip/schedule")
@Api(value = "旅游团-新增团期", tags = "旅行团-新增团期")
public class SeriesTripScheduleController extends BaseController {

    @DubboReference
    private ISeriesTripScheduleService seriesTripScheduleService;

    @ApiOperation(value = "查询团期详情", notes = "查询团期详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询团期详情", operDesc = "查询行程-基本资料")
    @GetMapping("/get/trip/schedule/{id}")
    public Result<SeriesTripScheduleDTO> getTripScheduleId(@PathVariable Long id) {
        return Result.success(seriesTripScheduleService.getTripScheduleId(id));
    }
    
    @ApiOperation(value = "查询线下自营预订团期详情", notes = "查询线下自营预订团期详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询线下自营预订团期详情", operDesc = "查询线下自营预订团期详情")
    @GetMapping("getTripScheduleIdBookOfflineSelf/{id}")
    public Result<SeriesTripScheduleDTO> getTripScheduleIdBookOfflineSelf(@PathVariable Long id) {
        return Result.success(seriesTripScheduleService.getTripScheduleIdBookOfflineSelf(id, getCurrentUserId()));
    }
    
    @ApiOperation(value = "查询线下代理预订团期详情", notes = "查询线下代理预订团期详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询线下代理预订团期详情", operDesc = "查询线下代理预订团期详情")
    @PostMapping("getTripScheduleIdBookOfflineAgent")
    public Result<SeriesTripScheduleDTO> getTripScheduleIdBookOfflineAgent(@RequestBody @Valid TripScheduleBookOfflineAgentFindReq req) {
        return Result.success(seriesTripScheduleService.getTripScheduleIdBookOfflineAgent(req));
    }

    @ApiOperation(value = "查询团期价格（提供订单下单--加幅）", notes = "查询团期价格（提供订单下单--加幅）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询团期详情", operDesc = "查询行程-基本资料")
    @PostMapping("/find/schedule")
    public Result<SeriesTripScheduleDTO> findTripScheduleId(@RequestBody @Valid FindTripScheduleReq findTripScheduleReq) {
        return Result.success(seriesTripScheduleService.findTripScheduleId(findTripScheduleReq, getCurrentUser()));
    }


    @ApiOperation(value = "查询团期详情详细接口（行程信息、补充信息等）", notes = "查询团期详情详细接口（行程信息、补充信息等）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询团期详情详细接口（行程信息、补充信息等）", operDesc = "查询团期详情详细接口（行程信息、补充信息等）")
    @GetMapping("/get/trip/schedule/details/{id}")
    public Result<TripScheduleDetailsDTO> getTripScheduleDetailsId(@PathVariable Long id) {
        return Result.success(seriesTripScheduleService.getTripScheduleDetailsId(id));
    }

    @ApiOperation(value = "修改团已收客人数", notes = "修改团已收客人数")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.UPDATE, operTitle = "修改团已收客人数", operDesc = "修改团已收客人数")
    @PostMapping("/update/schedule/passenger/num")
    public Result updateSchedulePassengerNum(@RequestBody @Valid UpdateSchedulePassengerReq updateSchedulePassengerReq) {
        return Result.success(seriesTripScheduleService.updateSchedulePassengerReq(updateSchedulePassengerReq, getCurrentUser()));
    }

    @ApiOperation(value = "订单返回相应库存", notes = "订单返回相应库存")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.UPDATE, operTitle = "订单返回相应库存", operDesc = "订单返回相应库存")
    @PostMapping("/update/schedule/num")
    public Result updateScheduleNum(@RequestBody @Valid UpdateScheduleNumReq updateScheduleNumReq) {
        seriesTripScheduleService.updateScheduleNum(updateScheduleNumReq);
        return Result.success();
    }


    @ApiOperation(value = "新增团期", notes = "新增团期")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.ADD, operTitle = "新增团期", operDesc = "新增团期")
    @PostMapping("/add/schedule")
    public Result addSchedule(@RequestBody @Valid List<AddTripScheduleReq> addTripScheduleReqList) {
        seriesTripScheduleService.addSchedule(addTripScheduleReqList, getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "修改团期", notes = "修改团期")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.UPDATE, operTitle = "修改团期", operDesc = "修改团期")
    @PostMapping("/update/schedule")
    public Result updateSchedule(@RequestBody @Valid UpdateTripScheduleReq updateTripScheduleReq) {
        seriesTripScheduleService.updateSchedule(updateTripScheduleReq, getCurrentUser());
        return Result.success();
    }


    @ApiOperation(value = "查询列表（提供订单列表查询）", notes = "查询列表（提供订单列表查询）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询列表（提供订单列表查询）", operDesc = "查询列表（提供订单列表查询）")
    @PostMapping("/get/trip/schedule/list")
    public Result<List<TripScheduleListDTO>> getTripScheduleList(@RequestBody @Valid TripScheduleListIdReq tripScheduleListIdReq) {
        return Result.success(seriesTripScheduleService.getTripScheduleList(tripScheduleListIdReq));
    }


    @ApiOperation(value = "查询列表", notes = "查询列表）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询列表", operDesc = "查询列表")
    @PostMapping("/list")
    public Result<PageResponse<TripScheduleDTO>> scheduleList(@RequestBody @Valid TripScheduleListQueryReq tripScheduleListQueryReq) {
        return Result.success(seriesTripScheduleService.scheduleList(tripScheduleListQueryReq, getCurrentUser(getRequest())));
    }
    
    @ApiOperation(value = "运营中心查询列表", notes = "运营中心查询列表）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "运营中心查询列表", operDesc = "运营中心查询列表")
    @PostMapping("/listForIotc")
    public Result<PageResponse<TripScheduleDTO>> scheduleListForIotc(@RequestBody @Valid TripScheduleListQueryReq tripScheduleListQueryReq) {
        return Result.success(seriesTripScheduleService.scheduleListForIotc(tripScheduleListQueryReq, getCurrentUser(getRequest())));
    }

    @ApiOperation(value = "（行程详情），旅行团团期", notes = "行程详情，旅行团团期）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "行程详情，旅行团团期", operDesc = "行程详情，旅行团团期")
    @PostMapping("/schedule/departure/list")
    public Result<List<ScheduleCalendarDTO>> scheduleDepartureList(@RequestBody ScheduleDepartureListQueryReq scheduleDepartureListQueryReq) {
        return Result.success(seriesTripScheduleService.scheduleDepartureList(scheduleDepartureListQueryReq));
    }


    @ApiOperation(value = "查询团成本", notes = "查询团成本）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询团成本", operDesc = "查询团成本")
    @GetMapping("/get/cost/{scheduleId}")
    public Result<TripScheduleCostDTO> getCost(@PathVariable Long scheduleId) {
        return Result.success(seriesTripScheduleService.getCost(scheduleId));
    }

    @ApiOperation(value = "修改团状态(保留，不成团)", notes = "修改团状态(保留，不成团)")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "修改团状态(保留，不成团)", operDesc = "修改团状态(保留，不成团)")
    @PostMapping("/update/schedule/status")
    public Result updateScheduleStatus(@RequestBody @Valid UpdateTripScheduleStatusReq updateTripScheduleStatusReq) {
        seriesTripScheduleService.updateScheduleStatus(updateTripScheduleStatusReq);
        return Result.success();
    }

    @ApiOperation(value = "修改团状态(1:停售 2：完团)", notes = "修改团状态(1:停售 2：完团)")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "修改团状态(1:停售 2：完团)", operDesc = "修改团状态(1:停售 2：完团)")
    @PostMapping("/update/status")
    public Result updateStatus(@RequestBody @Valid UpdateTripStatusReq updateTripStatusReq) {
        seriesTripScheduleService.updateTripStatusReq(updateTripStatusReq);
        return Result.success();
    }

    @ApiOperation(value = "查询是否满足成团", notes = "查询是否满足成团)")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "查询是否满足成团", operDesc = "查询是否满足成团")
    @GetMapping("/group/{id}")
    public Result<TripScheduleGroupDTO> group(@PathVariable Long id) {
        return Result.success(seriesTripScheduleService.group(id));
    }

    @ApiOperation(value = "通知派团", notes = "通知派团")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.UPDATE, operTitle = "通知派团", operDesc = "通知派团")
    @PostMapping("/notice")
    public Result notice(@RequestBody @Valid TripScheduleNoticeReq tripScheduleNoticeReq) {
        seriesTripScheduleService.notice(tripScheduleNoticeReq, getCurrentUser());
        return Result.success();
    }

    @ApiOperation(value = "茶会通知", notes = "茶会通知")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.UPDATE, operTitle = "通知派团", operDesc = "通知派团")
    @PostMapping("/tea/party")
    public Result teaParty(@RequestBody @Valid TripScheduleTeaPartyReq tripScheduleTeaPartyReq) {
        seriesTripScheduleService.teaParty(tripScheduleTeaPartyReq, getCurrentUser());
        return Result.success();
    }

    @ApiOperation(value = "调整项目(提供订单--调整项目功能)", notes = "提供订单--调整项目功能")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "调整项目", operDesc = "调整项目")
    @GetMapping("/adjustment/items/{id}")
    public Result<List<TripScheduleAdjustmentDTO>> adjustmentItems(@PathVariable Long id) {
        return Result.success(seriesTripScheduleService.adjustmentItems(id));
    }


    @ApiOperation(value = "（旅行团预订）查询列表", notes = "（旅行团预订）查询列表）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "（旅行团预订）查询列表", operDesc = "（旅行团预订）查询列表")
    @PostMapping("/schedule/book/list")
    public Result<PageResponse<TripScheduleDTO>> scheduleBookList(@RequestBody @Valid TripScheduleListQueryReq tripScheduleListQueryReq) {
        return seriesTripScheduleService.scheduleBookList(tripScheduleListQueryReq, getCurrentUser(getRequest()));
    }

    @ApiOperation(value = "（查询代理是否能下单）", notes = "查询代理是否能下单）")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "（旅行团预订）查询列表", operDesc = "（旅行团预订）查询列表")
    @PostMapping("/schedule/agent")
    public Result<Boolean> scheduleAgent(@RequestBody @Valid TripScheduleAgentReq tripScheduleAgentReq) {
        return Result.success(seriesTripScheduleService.scheduleAgent(tripScheduleAgentReq, getCurrentUser(getRequest())));
    }

    @ApiOperation("导出团期列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "导出团期列表", operDesc = "导出团期列表")
    @PostMapping("/exportScheduleList")
    public void exportScheduleList(@RequestBody @Valid TripScheduleListQueryReq req) {
    	List<TripScheduleDTO> list = seriesTripScheduleService.exportScheduleList(req, getCurrentUser());
    	OutputStream outputStream = ServletUtils.getDownloadOutputStream("旅行团列表.xlsx", "application/vnd.ms-excel");
    	String col = "團狀態, scheduleStatus, 團號, tripNumber, 行程碼, tripCode, 出發日期, departureDate, 回程日期, backDate, 天數, travelDays, 行程標題, title"
    			+ ", 最大成團人数, maxPeople, 最小成團人数, minPeople, 餘位, title, 留位数, reservedSeatsNum, 團費-直客價(HKD), groupCustomerPrice"
    			+ ", 毛利率(直客價), customerGrossProfitRate, 團費-代理價(HKD), groupAgencyPrice, 毛利率(代理價), agencyGrossProfitRate, 負責人員, chargePersonName";
    	ExcelExportUtils.writeForPdcf(outputStream, list, col);
    }
    
    @ApiOperation("查询系列团预订可下单的代理商列表")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "（旅行团预订）查询列表", operDesc = "（旅行团预订）查询列表")
    @PostMapping("querAgentListByBook")
    public Result<List<SeriesAgentByBookDTO>> querAgentListByBook(@RequestBody @Valid SeriesAgentListByBookQueryReq req) {
		return seriesTripScheduleService.querAgentListByBook(req);
	}
    
    @ApiOperation("自营下单查询团期详情")
    @OperationLogAnnotation(operModul = OperationLogModel.TOUR, operType = OperationLogType.QUERY, operTitle = "自营下单查询团期详情", operDesc = "自营下单查询团期详情")
    @GetMapping("findTripScheduleBySelf/{id}")
    public Result<SeriesTripScheduleDTO> findTripScheduleBySelf(@PathVariable Long id) {
        return seriesTripScheduleService.findTripScheduleBySelf(id);
    }

    @ApiOperation(value="操作订单-调整项目列表", notes="操作订单-调整项目列表(叶冠峰)")
    @OperationLogAnnotation(operModul = OperationLogModel.CUSTOM, operType = OperationLogType.QUERY, operTitle = "操作订单-调整项目列表", operDesc = "操作订单-调整项目列表")
    @GetMapping("/adjust-project-list/{id}")
    public Result<TripScheduleAdjustProjectDTO> adjustProjectList(@PathVariable Long id) {
        return seriesTripScheduleService.adjustProjectList(id);
    }

}
